<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="快递名称">
                <a-input v-model="queryParam.name" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="() => { this.formVisible = true }">新建</a-button>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      >
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical"/>

          <a-popconfirm placement="top" @confirm="confirmDelete(record)">
            <template slot="title">
              确定要删除吗?
            </template>
            <a>删除</a>
          </a-popconfirm>
        </template>
      </span>
      </s-table>
    </a-card>

    <courier-company-form :visible="formVisible" :data="formRaw" @visible="formVisibleEvent" @refreshList="refreshList"></courier-company-form>
  </div>
</template>

<script>
  import { Ellipsis, STable } from '@/components'
  import { getCourierCompanyList, deleteCourierCompany } from '@/api/system'
  import CourierCompanyForm from '@/views/system/courier_company/Form'

  export default {
    name: 'List',
    components: {
      CourierCompanyForm,
      STable,
      Ellipsis
    },
    data () {
      return {
        formVisible: false,
        formRaw: {},
        mdl: {},
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '快递名称',
            dataIndex: 'name'
          },
          {
            title: '排序号',
            dataIndex: 'sort_id'
          },
          {
            title: '更新时间',
            dataIndex: 'updated_at'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return getCourierCompanyList(Object.assign(parameter, this.queryParam)).then(res => {
            return res
          })
        },
        selectedRowKeys: [],
        selectedRows: []
      }
    },
    created () {

    },
    methods: {
      handleEdit (record) {
        this.formVisible = true
        this.formRaw = record
      },
      confirmDelete (record) {
        deleteCourierCompany(record.id).then(() => this.$refs.table.refresh())
      },
      formVisibleEvent (val) {
        this.formVisible = val
        this.formRaw = {}
      },
      refreshList () {
        this.$refs.table.refresh()
      }
    }
  }
</script>
